<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * Class CommonController
 * @package App\Http\Controllers\Admin
 * 公共类
 */
class CommonController extends Controller
{
    protected static $Page=1;
    protected static $Limit=10;
    protected function ReturnJson($code,$msg,$data){
        $data=['code'=>$code,'msg'=>$msg,'data'=>$data];
        return json_encode($data);
    }
   protected function GetRandStr($length){
        $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890';
        $randStr = str_shuffle($str);//打乱字符串
        $rands= substr($randStr,0,$length);
        return $rands;
    }


    /**
     * @param $user_id
     * @return array
     * 获取用户权限
     */
    protected function GetPermissionUser($user_id){
        $res=DB::table('user as a')
            ->leftJoin('user_role as b','a.id','=','b.user_id')
            ->leftJoin('role as c','c.id','=','b.role_id')
            ->leftJoin('permission_role as d','d.role_id','=','c.id')
            ->leftJoin('permission as e','e.id','=','d.permission_id')
            ->where('a.delete_flag','=',0)
            ->where('a.status','=',0)
            ->where('b.delete_flag','=',0)
            ->where('b.status','=',0)
            ->where('c.delete_flag','=',0)
            ->where('c.status','=',0)
            ->where('d.delete_flag','=',0)
            ->where('d.status','=',0)
            ->where('e.delete_flag','=',0)
            ->where('e.status','=',0)
            ->where('a.id','=',$user_id)
            ->where('e.is_show','=',1)
            ->select('e.id as per_id','e.pid as p_id','e.name as per_name','e.info as per_info','e.route as per_route','e.title as per_title','e.icon as per_icon','e.menu_path as per_menu_path','e.menu_name as per_menu_name','e.component as per_component','e.is_show as per_is_show')
            ->get()
            ->toArray();
        return $res;
    }

    /**
     * @param Request $request
     * @return mixed
     * 获取用户id
     */
    public function getUserId($token){
        $jwtAuth=JwtAuth::getInstance();
        return $jwtAuth->setToken($token)->getUid();
    }


}
